/*----------------------------------------------------------------------
  - File name      : STC8Ax_SPI.c
  - Author         : Quark Team
  - Update date    : 2020-09-06                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: SPI module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_SPI.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/* None */
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: SPI init function                        |
| @param      : SPIInit_Type *SPIx                       |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE SPI_Init(SPIInit_Type *SPIx)
{
	SPDAT = 0;
  SPSTAT = SPIF | WCOL;          
  SPCTL = (SPCTL & 0x6B) | (SPIx -> Type);
  SPCTL = (SPCTL & 0xFC) | (SPIx -> ClkSrc);
  SPCTL = (SPCTL & 0xF3) | (SPIx -> Mode << 2);
  SPCTL = (SPCTL & 0xDF) | (SPIx -> Tran << 5);
  SPCTL = (SPCTL & 0xBF) | (SPIx -> Run << 6);  
  return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: SPI send data function                   |
| @param      : uint8 Dat                                |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE SPI_Send_Data(uint8 Dat)
{
  SPDAT = Dat;				      //Data register assignment
  while (!(SPSTAT & 0x80));  //Query completion flag
  SPSTAT = 0xC0;         //Clear interrupt flag
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: SPI receive data function                |
| @param      : uint8 Dat                                |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE SPI_Rev_Data(uint8 * Dat)
{
  while (!(SPSTAT & 0x80));  //Query completion flag
  SPSTAT = 0xC0;         //Clear interrupt flag
  *Dat = SPDAT;				      //Data register assignment
	return FSC_SUCCESS;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



